package com.ssm.controller;

import com.ssm.pojo.Customer;
import com.ssm.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @RequestMapping("/customer_list")
    public String customer_list(ModelMap map){
        List<Customer> customerList=customerService.getAllCustomers();
        map.addAttribute("customerList",customerList);
        return "customer_list";
    }
    @RequestMapping("/query")
    public String customer_query(){ return "customer_query"; }
    @RequestMapping("/add")
    public void add(Customer customer, HttpServletResponse resp){
        System.out.println("请求添加用户信息"+customer);
        int n=customerService.addCustomer(customer);
        System.out.println("添加了"+n+"条客户信息");
        try {
            PrintWriter out=resp.getWriter();
            out.print(n);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/update")
    public void update(Customer customer, HttpServletResponse resp){
        System.out.println("请求更新用户信息"+customer);
        int n=customerService.update(customer);
        System.out.println("更新了"+n+"条客户信息");
        try {
            PrintWriter out=resp.getWriter();
            out.print(n);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/delete")
    public void delete(String card,HttpServletResponse resp){
        System.out.println("请求删除用户信息"+card);
        int n=customerService.delete(card);
        System.out.println("删除了"+n+"条客户信息");
        try {
            PrintWriter out=resp.getWriter();
            out.print(n);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
